/*
 * @Author: 李无敌
 * @Date: 2025-07-25 17:01:09
 * @LastEditors: 李无敌
 * @LastEditTime: 2025-08-14 08:54:35
 * @FilePath: \nest-base\src\common\entity\base.entity.ts
 */
import {
  PrimaryGeneratedColumn,
  Column,
  BeforeInsert,
  BeforeUpdate,
} from 'typeorm';

export abstract class BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({
    name: 'created_at',
    type: 'bigint',
    comment: '创建时间',
    nullable: true,
  })
  createdAt: number;

  @Column({
    name: 'updated_at',
    type: 'bigint',
    comment: '更新时间',
    nullable: true,
  })
  updatedAt: number;

  @Column({
    name: 'is_deleted',
    type: 'boolean',
    default: false,
    comment: '是否删除',
  })
  isDeleted: boolean;

  /**
   * 在插入前自动设置创建时间和更新时间
   */
  @BeforeInsert()
  setTimestampsOnInsert() {
    const now = Date.now();
    this.createdAt = now;
    this.updatedAt = now;
  }

  /**
   * 在更新前自动设置更新时间
   */
  @BeforeUpdate()
  setTimestampsOnUpdate() {
    this.updatedAt = Date.now();
  }
} 